package org.dromara.common.sms.lxk;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.dromara.common.core.constant.HttpStatus;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.sms.lxk.config.LxkSmsConfig;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
import org.dromara.sms4j.provider.service.AbstractSmsBlend;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.Executor;

/**
 * @author heyi
 * 2024/8/13
 */
public class LxkSmsImpl extends AbstractSmsBlend<LxkSmsConfig> {

    /**
     * AlibabaSmsImpl
     * <p>构造器，用于构造短信实现模块
     *
     * @author :Wind
     */
    public LxkSmsImpl(LxkSmsConfig config, Executor pool, DelayedTime delayedTime) {
        super(config, pool, delayedTime);
    }

    public LxkSmsImpl(LxkSmsConfig config) {
        super(config);
    }

    @Override
    public String getSupplier() {
        return "lxk";
    }

    @Override
    public SmsResponse sendMessage(String phone, String message) {
        throw new ServiceException("lxk sms not support");
    }

    @Override
    public SmsResponse sendMessage(String phone, LinkedHashMap<String, String> messages) {
        return sendMessage(phone, getConfig().getTemplateId(), messages);
    }

    @Override
    public SmsResponse sendMessage(String phone, String templateId, LinkedHashMap<String, String> messages) {
        LxkSmsConfig config = getConfig();
        String httpsUrl = config.getRequestUrl();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mobile", phone);
        jsonObject.put("smsSignId", config.getSignature());
        jsonObject.put("templateId", templateId);
        jsonObject.put("paramMap", messages);
        String result = HttpUtil.createPost(httpsUrl)
            .header("access_key", config.getAccessKeyId())
            .header("secret_key", config.getAccessKeySecret())
            .contentType("application/json")
            .body(jsonObject.toJSONString(0)).execute().body();
        SmsResponse response = new SmsResponse();
        response.setConfigId(config.getConfigId());
        JSONObject t = JSONUtil.parseObj(result);
        if (t.get("code").equals(HttpStatus.SUCCESS)) {
            response.setSuccess(true);
        } else {
            response.setSuccess(false);
        }
        response.setData(t);
        return response;
    }

    @Override
    public SmsResponse massTexting(List<String> phones, String message) {
        throw new ServiceException("lxk sms not support");
    }

    @Override
    public SmsResponse massTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
        throw new ServiceException("lxk sms not support");
    }


}
